import {createRouter, createWebHashHistory, RouteRecordRaw, useRouter} from 'vue-router';
import {useUserStore} from "@/store/user";
import {usePermissStore} from '@/store/permiss';
import Home from '../views/home.vue';

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        redirect: '/home',
        children: [
            {
                path: '/home',
                name: 'home',
                meta: {
                    title: '首页',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '@/views/Home/index.vue'),
            },
            // 项目管理
            {
                path: '/projectMgt/index',
                name: 'projectIndex',
                meta: {
                    title: '小区管理',
                    permiss: ['superAdmin'],
                },
                component: () => import(/* webpackChunkName: "table" */ '@/views/ProjectMgt/index.vue'),
            },
            {
                path: '/userMgt/index',
                name: 'userIndex',
                meta: {
                    title: '用户管理',
                    permiss: ['admin', 'superAdmin'],
                },
                component: () => import(/* webpackChunkName: "table" */ '@/views/UserMgt/index.vue'),
            },
            {
                path: '/parkingMgt/index',
                name: 'parkingIndex',
                meta: {
                    title: '车场管理',
                },
                component: () => import(/* webpackChunkName: "table" */ '@/views/ParkingMgt/parking.vue'),
            },
            {
                path: '/parkingMgt/lane',
                name: 'parkingLine',
                meta: {
                    title: '车道管理',
                },
                component: () => import(/* webpackChunkName: "table" */ '@/views/ParkingMgt/lane.vue'),
            },
            {
                path: '/parkingMgt/car',
                name: 'parkingCar',
                meta: {
                    title: '车辆管理',
                },
                component: () => import(/* webpackChunkName: "table" */ '@/views/ParkingMgt/car.vue'),
            },
        ],
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录',
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/login.vue'),
    },
    {
        path: '/403',
        name: '403',
        meta: {
            title: '没有权限',
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/403.vue'),
    },
    {
        path: '/404',
        name: '404',
        meta: {
            title: '页面不存在',
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/404.vue'),
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | 车场管理系统`;
    const user = useUserStore();
    const permiss = usePermissStore();
    if (!user.userInfo?.id && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permiss && !((to.meta.permiss as any).indexOf(permiss.userRole) > -1)) {
        // 如果没有权限，则进入403
        next('/403');
    } else {
        next();
    }
});

export default router;